package com.qianfeng.controller;

import com.github.pagehelper.PageInfo;
import com.qianfeng.dto.Page;
import com.qianfeng.entity.Role;
import com.qianfeng.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;

    @RequestMapping("/jumpAddRole")
    public String jumpAddRole(){
        return "11_01_role_management_add";
    }

   /* @RequestMapping("/login")
    public String getRoleList(Map map){
        List<Role> roleList = roleService.getRoleList();
        map.put("roles",roleList);
        return "11_role_management";
    }*/
    @RequestMapping("/getRolePage")
    public String getRolePage(Integer pageNo,Integer pageSize,Map map){
     /*   int pageNo_=1;
        int pageSize_=2;

        if(pageNo!=null){
            pageNo_=pageNo;
        }
        if(pageSize!=null){
            pageSize_=pageSize;
        }
        List<Role> rolePage = roleService.getRolePage(pageNo_, pageSize_);
        map.put("roles",rolePage);*/
        return "11_role_management";
    }

    @RequestMapping("/addRole")
    public String addRole(Role role){
        System.out.println(role);
        roleService.addRole(role);
        return "redirect:/getRolePage";
    }
    @RequestMapping("/deleteRole/{roleId}")
    public String deleteRole(@PathVariable("roleId") Integer roleId){
        roleService.deleteRole(roleId);
        return "forward:/getRolePage";
    }
    @RequestMapping("/jumpUpdateRole/{roleId}")
    public String jumpUpdateRole(@PathVariable("roleId") Integer roleId, Map map){
        Role role = roleService.getRoleByRoleId(roleId);
        map.put("role",role);
        return "11_02_role_management_update";
    }
    @RequestMapping("/updateRole")
    public String updateRole(Role role){
        roleService.updateRole(role);
        return "redirect:/getRolePage";
    }
    @ResponseBody
    @RequestMapping("/getPage")
    public Page getPage(Integer pageNo,Integer pageSize){
        List<Role> rolePage = roleService.getRolePage(pageNo, pageSize);
        PageInfo pageInfo = new PageInfo(rolePage);
        Page page = new Page();
        page.setObj(pageInfo.getList());
        page.setPageCount(pageInfo.getPages());
        page.setHasNext(pageInfo.isHasNextPage());
        return page;
    }
    @ResponseBody
    @RequestMapping("/getLikeRoleList")
    public  List<Role> getLikeRoleList(String roleName){
        //System.out.println("roleName==="+roleName);
        return roleService.getLikeRoleList(roleName);
    }

}
